//
//  DDSeverSubViewController.m
//  PrivateCloud
//
//  Created by sarah doo on 13-10-14.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDSeverSubViewController.h"
#import "DDSeverTableCell.h"
#import "DDSeverViewController.h"
#import "DDSeverTableModel.h"
#import "MBProgressHUD.h"
#import "DDDealFileCallback.h"
#import "DDFileInfo.h"
#import "DDCommentDic.h"
#import "DDVerifyThread.h"
#import "DDFolderInfo.h"
#import "DDTCPListenThread.h"
#import "DDSuccessViewController.h"
#import "UIColor+.h"

#define ALL_SELECT_TAG 100001
@interface DDSeverSubViewController ()

@end

@implementation DDSeverSubViewController
@synthesize resultArray, table,titleName,type,fileDataSource,from,progressHUD;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    isAllSelect = NO;
    self.resultArray = [[NSMutableArray alloc]init];
    for (DDFileInfo* fileInfo in self.fileDataSource) {
        DDSeverTableModel *tableModel = [[DDSeverTableModel alloc]init];
        tableModel.titleName = [fileInfo fileName];
        tableModel.isSelected = NO;
        tableModel.uid = [fileInfo fileId];
        [self.resultArray addObject:tableModel];
        [tableModel release];
    }
    [self createTopView];
	// Do any additional setup after loading the view.
}


- (void)createTopView {
    UIView *linkView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)];
//    linkView.backgroundColor = [UIColor greenColor];
    linkView.backgroundColor = [UIColor colorWithString:@"#273826"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"ico_back.png"];
    backButton.frame = CGRectMake(10, 4, 32, 32);
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:image forState:UIControlStateNormal];
    [linkView addSubview:backButton];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-25, 5, self.view.frame.size.width - image.size.width - 80, 30)];
    label.font = [UIFont systemFontOfSize:15];
    label.text = titleName;
    label.textColor = [UIColor whiteColor];
    [linkView addSubview:label];
    [label release];
    [self.view addSubview:linkView];
    
    UIButton *allSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    allSelectButton.frame = CGRectMake(self.view.frame.size.width - 40, 0, 80, 30);
    allSelectButton.backgroundColor = [UIColor clearColor];
    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, allSelectButton.frame.size.width, allSelectButton.frame.size.height)];
    buttonLabel.textColor = [UIColor whiteColor];
    buttonLabel.font = [UIFont systemFontOfSize:15];
    buttonLabel.backgroundColor = [UIColor clearColor];
    buttonLabel.tag = ALL_SELECT_TAG;
    buttonLabel.text = @"全选";
    [allSelectButton addTarget:self action:@selector(selectAllItem:) forControlEvents:UIControlEventTouchUpInside];
    [allSelectButton addSubview:buttonLabel];
    [buttonLabel release];
    [linkView addSubview:allSelectButton];
    [linkView release];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake( 5, 60, self.view.frame.size.width - 10, self.view.frame.size.height - 120) style:UITableViewStylePlain];
    self.table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    //    [tableView release];
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    [footerView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:footerView];
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //sureButton.frame = CGRectMake(self.view.frame.size.width/2 -25, 0, 80, 30);
    sureButton.frame = CGRectMake(0, 0, footerView.frame.size.width, footerView.frame.size.height);
    sureButton.backgroundColor = [UIColor clearColor];
    UILabel *sureLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 -25, 0, 80, 30)];
    sureLabel.textColor = [UIColor whiteColor];
    sureLabel.font = [UIFont systemFontOfSize:15];
    sureLabel.backgroundColor = [UIColor clearColor];
    sureLabel.text = @"确   定";
    [sureButton addTarget:self action:@selector(sureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [sureButton addSubview:sureLabel];
    [sureLabel release];
    [footerView addSubview:sureButton];
    [footerView release];
}

//确定按钮被点击的情况
- (void)sureButtonPressed:(id)sender {
    //获取被选中的控件的文件的ID
    NSMutableArray* selectedFileinfos = [[NSMutableArray alloc]init];
    for (int i = 0 ; i < [self.resultArray count]; i++) {
        DDSeverTableModel* model = [self.resultArray objectAtIndex:i];
        if(model.isSelected){
            [selectedFileinfos addObject:[self.fileDataSource objectAtIndex:i]];
        }
    }
    if([selectedFileinfos count] == 0){
        [self showSelectAlert:type];
        return;
    }
    
    if ([selectedFileinfos count]>0) {
        if (type == SERVER_CODE) {
            //开始执行下载操作
            [[DDVerifyThread sharedVerifyThread]downFileList:selectedFileinfos];
            [self showProgressDialogWithCode:SERVER_CODE];
            [DDTCPListenThread sharedTCPListener].dealFileCallback = self;
        }
        else if (type == LOCAL_CODE){
            //开始执行上传操作
            [[DDVerifyThread sharedVerifyThread]uploadFileList:selectedFileinfos];
//            [self showProgressDialogWithCode:LOCAL_CODE];
            progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            progressHUD.mode = MBProgressHUDModeDeterminate;
            progressHUD.opacity = 0.6;
            progressHUD.labelFont = [UIFont boldSystemFontOfSize:14];
            progressHUD.detailsLabelFont = [UIFont boldSystemFontOfSize:12];
            progressHUD.detailsLabelText = @"上传";
            progressHUD.removeFromSuperViewOnHide = YES;
            [DDVerifyThread sharedVerifyThread].dealFileCallback = self;
        }
    }
}

-(void)selectAllItem:(id)sender{
    UILabel* buttonLabel = [self.view viewWithTag:ALL_SELECT_TAG];
    if(isAllSelect == YES){
        //取消全选
        for (DDSeverTableModel* model in self.resultArray) {
            model.isSelected = NO;
        }
        isAllSelect = NO;
        buttonLabel.text = @"全选";
    }
    else{
        //全选
        for (DDSeverTableModel* model in self.resultArray) {
            model.isSelected = YES;
        }
        isAllSelect = YES;
        buttonLabel.text = @"取消";
    }
    [self.table reloadData];
}

-(void)showSelectAlert:(int)code{
    NSString* labelString = nil;
    if(code == SERVER_CODE){
        labelString = @"请选择需要下载的文件";
    }
    else{
        labelString = @"请选择需要上传的文件";
    }
    progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:progressHUD];

    progressHUD.mode = MBProgressHUDModeCustomView;
    progressHUD.labelText= labelString;
    [progressHUD show:YES];
    [progressHUD hide:YES afterDelay:3];
}

- (void)showProgressDialogWithCode:(int)code{
    NSString* labelString = nil;
    if(code == SERVER_CODE){
        labelString = @"正在下载数据..";
    }
    else{
        labelString = @"正在上传数据..";
    }
    progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    progressHUD.mode = MBProgressHUDModeDeterminate;
    progressHUD.opacity = 0.6;
    progressHUD.labelFont = [UIFont boldSystemFontOfSize:14];
    progressHUD.detailsLabelFont = [UIFont boldSystemFontOfSize:12];
    progressHUD.detailsLabelText = labelString;
    progressHUD.removeFromSuperViewOnHide = YES;
}

- (void)dismisProgressDialog{
    progressHUD.hidden = YES;
//    [progressHUD release];
}

#pragma - mark tableViewDelegate

//返回当前列表对象的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [resultArray count];
}

//每次创建一个子View时，会运行该方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"cellID";
    DDSeverTableCell *cell = [[[DDSeverTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
    cell.model = [self.resultArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    DDSeverTableModel *model = [self.resultArray objectAtIndex:indexPath.row];
    model.isSelected = !model.isSelected;
    [self.resultArray removeObjectAtIndex:indexPath.row];
    [self.resultArray insertObject:model atIndex:indexPath.row];
    [tableView reloadData];
}

- (void)backButtonPressed:(id)sender {
    if (from == 1) {
        DDSuccessViewController* success = [[DDSuccessViewController alloc]initWithNibName:nil bundle:nil];
        [self presentViewController:success animated:NO completion:nil];
        [success release];
    }
    else{
        DDSeverViewController *sever = [[DDSeverViewController alloc] initWithNibName:nil bundle:nil];
        [self presentViewController:sever animated:NO completion:nil];
        [sever release];
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//对文件的上传和下载的处理的回调函数 code是0代表成功，code是1代表失败，code是2代表取消
-(void)onDealFileComplete:(DDFileInfo *)info andCode:(int)code{
    [self dismisProgressDialog];
}

-(void)onProgressChanged:(int)progress{
    float trueProgress = progress/100.0f;
    [progressHUD setProgress:trueProgress];
//    progressHUD.progress = trueProgress;
    NSLog(@"VIEW----------%f",trueProgress);
}
@end
